<?PHP
include('config/config.php');
?>
<?PHP
	function uploadTxtData_Inventory($filename){	
		$tbl_name=$cfg["tbl"]["inventory"];
		$file = fopen( $filename, "r" ) or die ('ERROR: fopen()');
		
		$filesize = filesize( $filename );
		$filetext = fread( $file, $filesize );
		
		fclose( $file);
		$content = explode("\n", $filetext);
		foreach($content as $line)
		{
			list($name, $category, $manuf, $barcode, $costprice, 
			$currentStock, $minStock, $bundleunit) = explode(":",$line);
			$name = db_escape_string($name);

			$query = "INSERT INTO $tbl_name VALUES('".$barcode."', '".$name."', '".$costprice."', '".$category."', 
												'".$manuf."', '".$currentStock."', '".$minStock."', '".$bundleunit."');";
			$result = mysql_query($query) or die(mysql_error());
		}	
	}
?>

<?PHP
	session_start();
	if(isset($_SESSION['datatxtfile']))
	{
		$allowedExts = array("txt", "csv");
		$extension = end(explode(".", $_SESSION["datatxtfile"]["name"]));
		if(in_array($extension, $allowedExts))
		{
			if ($_FILES["datatxtfile"]["error"] > 0) echo "Error: " . $_FILES["datatxtfile"]["error"];
			else {
				//uploadTxtData_Inventory($_SESSION["datatxtfile"]["tmp_name"]);
			}
		}
		else
			echo "Invalid File";
	}
	else
	{
		// $conn = mysql_connect($cfg["connection"]["host"], $cfg["connection"]["userName"], $cfg["connection"]["password"]) 
			// or die ('Error Cannot Connect to MySQL');
		// @mysql_select_db($cfg["connection"]["database"]) or die( "Unable to select database");
		
		// $query = "drop table product";
		// $result = mysql_query($query);
		// $query = "create table product (name varchar(100), category varchar(50), 
				  // manufacturer varchar(50), barcode int, costprice double, 
				  // currentstock int, minstock int, primary key (barcode));";
		// $result = mysql_query($query);
	}					
?>